/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Interfaces.ApparentModalI;
import Model.Playlist;
import Model.Playlistsong;
import Model.Song;
import java.awt.Frame;
import java.awt.Image;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import src.General;
import static src.General.dateFormater2;
import static src.General.dateFormater3;

/**
 *
 * 
 */
public class MngPlaylistXML extends javax.swing.JFrame implements ApparentModalI {

    private Playlists prFrmPlaylists;
    private MngPlaylist prFrmMngPlaylist;
    private General.XMLActionType prEnmXMLActionType;
    private EntityManager prEM;
    private String prUsername;
    private Model.Playlist prSelectedPlaylist;
    private final JFrame next = null;

    /**
     * Creates new form MngPlaylistXML
     */
    public MngPlaylistXML(Playlists frmPlaylists, MngPlaylist frmMngPlaylist, Playlist selectedPlaylist, General.XMLActionType enmXMLActionType, EntityManager em, String username) {
        try {
            this.prFrmPlaylists = frmPlaylists;
            this.prFrmMngPlaylist = frmMngPlaylist;
            this.prEM = em;
            this.prEnmXMLActionType = enmXMLActionType;
            this.prUsername = username;

            this.prSelectedPlaylist = selectedPlaylist;

            initComponents();

            Image applicationImage = ImageIO.read(getClass().getResource("/Images/note1.png"));
            this.setIconImage(applicationImage);

            switch (prEnmXMLActionType) {
                case Export:
                    this.setTitle(this.getTitle() + " - " + "Εξαγωγή Στοιχείων Λίστας σε XML");
                    this.btnSave.setText("Εξαγωγή");
                    break;
                case Import:
                    this.setTitle(this.getTitle() + " - " + "Εισαγωγή Τραγουδιών από XML");
                    this.btnSave.setText("Εισαγωγή");
                    break;
                default:
                    break;
            }

            this.btnSave.setEnabled(false);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnExit = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        txtFile = new javax.swing.JTextField();
        lblFile = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtOutputArea = new javax.swing.JTextArea();
        lblOutput = new javax.swing.JLabel();
        btnChooseFile = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Radio Station - Φόρμα Διαχείρισης Λίστας");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        btnExit.setText("Έξοδος");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        txtFile.setEditable(false);

        lblFile.setText("Αρχείο");

        txtOutputArea.setColumns(20);
        txtOutputArea.setRows(5);
        jScrollPane1.setViewportView(txtOutputArea);

        lblOutput.setText("Import Log Info");

        btnChooseFile.setText("Επιλογή Αρχείου");
        btnChooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseFileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(txtFile, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 257, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnSave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 211, Short.MAX_VALUE)
                                .addComponent(btnExit))
                            .addComponent(btnChooseFile, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFile)
                            .addComponent(lblOutput))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnChooseFile)
                .addGap(7, 7, 7)
                .addComponent(lblOutput)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
//        if (this.prFrmPlaylists != null) {
//            this.prFrmPlaylists.setEnabled(true);
//        }
//        if (this.prFrmMngPlaylist != null) {
//            this.prFrmMngPlaylist.setEnabled(true);
//        }
    }//GEN-LAST:event_formWindowClosing

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        try {
//            if (this.prFrmPlaylists != null) {
//                this.prFrmPlaylists.setEnabled(true);
//            }
//            if (this.prFrmMngPlaylist != null) {
//                this.prFrmMngPlaylist.setEnabled(true);
//            }
           General.closeWindowX(this);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnExitActionPerformed

    private void exportFile() {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            Element playlistElement = doc.createElement("Playlist");
            doc.appendChild(playlistElement);

            Attr playlistIDAttribute = doc.createAttribute("ID");
            playlistIDAttribute.setValue(prSelectedPlaylist.getPlaylistId().toString());
            playlistElement.setAttributeNode(playlistIDAttribute);

            Element playlistNameElement = doc.createElement("Name");
            playlistNameElement.appendChild(doc.createTextNode(prSelectedPlaylist.getPlaylistName()));
            playlistElement.appendChild(playlistNameElement);

            Element playlistDescriptionElement = doc.createElement("Description");
            if (prSelectedPlaylist.getDescription() != null) {
                playlistDescriptionElement.appendChild(doc.createTextNode(prSelectedPlaylist.getDescription()));
            }
            playlistElement.appendChild(playlistDescriptionElement);

            Element playlistCreatedByElement = doc.createElement("CreatedBy");
            playlistCreatedByElement.appendChild(doc.createTextNode(prSelectedPlaylist.getCreatedBy()));
            playlistElement.appendChild(playlistCreatedByElement);

            Element playlistCreatedDateElement = doc.createElement("CreatedDate");
            playlistCreatedDateElement.appendChild(doc.createTextNode(dateFormater2.format(prSelectedPlaylist.getCreatedDate())));
            playlistElement.appendChild(playlistCreatedDateElement);

            Element playlistModifiedByElement = doc.createElement("ModifiedBy");
            if (prSelectedPlaylist.getModifiedBy() != null) {
                playlistModifiedByElement.appendChild(doc.createTextNode(prSelectedPlaylist.getModifiedBy()));
            }
            playlistElement.appendChild(playlistModifiedByElement);

            Element playlistModifiedDateElement = doc.createElement("ModifiedDate");
            if (prSelectedPlaylist.getModifiedBy() != null) {
                playlistModifiedDateElement.appendChild(doc.createTextNode(dateFormater3.format(prSelectedPlaylist.getModifiedDate())));
            }
            playlistElement.appendChild(playlistModifiedDateElement);

            Element playlistSongsElement = doc.createElement("Songs");
            playlistElement.appendChild(playlistSongsElement);

            List<Playlistsong> objPlaylistSongsList = prSelectedPlaylist.getPlaylistsongList();
            for (Playlistsong objPlaylistSong : objPlaylistSongsList) {
                Element playlistSongElement = doc.createElement("Song");
                playlistSongsElement.appendChild(playlistSongElement);

                Attr playlistSongIDAttribute = doc.createAttribute("ID");
                playlistSongIDAttribute.setValue(String.valueOf(objPlaylistSong.getSongId().getSongId()));
                playlistSongElement.setAttributeNode(playlistSongIDAttribute);

                Element playlistSongTitleElement = doc.createElement("Title");
                playlistSongTitleElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getSongTitle()));
                playlistSongElement.appendChild(playlistSongTitleElement);

                Element playlistSongDurationElement = doc.createElement("Duration");
                playlistSongDurationElement.appendChild(doc.createTextNode(String.valueOf(objPlaylistSong.getSongId().getDuration())));
                playlistSongElement.appendChild(playlistSongDurationElement);

                Element playlistSongTrackNumberElement = doc.createElement("TrackNumber");
                playlistSongTrackNumberElement.appendChild(doc.createTextNode(String.valueOf(objPlaylistSong.getSongId().getTrackNumber())));
                playlistSongElement.appendChild(playlistSongTrackNumberElement);

                Element playlistSongOrderElement = doc.createElement("Order");
                playlistSongOrderElement.appendChild(doc.createTextNode(String.valueOf(objPlaylistSong.getOrderNum())));
                playlistSongElement.appendChild(playlistSongOrderElement);

                Element playlistSongAlbumElement = doc.createElement("Album");
                playlistSongElement.appendChild(playlistSongAlbumElement);

                Attr playlistSongAlbumIDAttribute = doc.createAttribute("ID");
                playlistSongAlbumIDAttribute.setValue(String.valueOf(objPlaylistSong.getSongId().getAlbumId().getAlbumId()));
                playlistSongAlbumElement.setAttributeNode(playlistSongAlbumIDAttribute);

                Element playlistSongAlbumTitleElement = doc.createElement("Title");
                playlistSongAlbumTitleElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getAlbumTitle()));
                playlistSongAlbumElement.appendChild(playlistSongAlbumTitleElement);

                Element playlistSongAlbumCompanyElement = doc.createElement("Company");
                playlistSongAlbumCompanyElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getCompanyId().getCompanyName()));
                playlistSongAlbumElement.appendChild(playlistSongAlbumCompanyElement);

                Element playlistSongAlbumTypeElement = doc.createElement("Type");
                playlistSongAlbumTypeElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getTypeId().getTypeName()));
                playlistSongAlbumElement.appendChild(playlistSongAlbumTypeElement);

                Element playlistSongAlbumDiskNumberElement = doc.createElement("DiskNumber");
                playlistSongAlbumDiskNumberElement.appendChild(doc.createTextNode(String.valueOf(objPlaylistSong.getSongId().getAlbumId().getDiskNumber())));
                playlistSongAlbumElement.appendChild(playlistSongAlbumDiskNumberElement);

                Element playlistSongAlbumReleaseDateElement = doc.createElement("ReleaseDate");
                playlistSongAlbumReleaseDateElement.appendChild(doc.createTextNode(dateFormater2.format(objPlaylistSong.getSongId().getAlbumId().getReleaseDate())));
                playlistSongAlbumElement.appendChild(playlistSongAlbumReleaseDateElement);

                Element playlistSongAlbumArtistElement = doc.createElement("Artist");
                playlistSongAlbumElement.appendChild(playlistSongAlbumArtistElement);

                if (objPlaylistSong.getSongId().getAlbumId().getArtistId() != null) {
                    Attr playlistSongAlbumArtistIDAttribute = doc.createAttribute("ID");
                    playlistSongAlbumArtistIDAttribute.setValue(String.valueOf(objPlaylistSong.getSongId().getAlbumId().getArtistId().getArtistId()));
                    playlistSongAlbumArtistElement.setAttributeNode(playlistSongAlbumArtistIDAttribute);

                    Element playlistSongAlbumArtistFirstnameElement = doc.createElement("Firstname");
                    playlistSongAlbumArtistFirstnameElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getFirstname()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistFirstnameElement);

                    Element playlistSongAlbumArtistLastnameElement = doc.createElement("Lastname");
                    playlistSongAlbumArtistLastnameElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getLastname()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistLastnameElement);

                    Element playlistSongAlbumArtistArtisticNameElement = doc.createElement("ArtisticName");
                    playlistSongAlbumArtistArtisticNameElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getArtisticName()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistArtisticNameElement);

                    Element playlistSongAlbumArtistSexElement = doc.createElement("Sex");
                    playlistSongAlbumArtistSexElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getSexId().getSexName()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistSexElement);

                    Element playlistSongAlbumArtistMusicGenreElement = doc.createElement("MusicGenre");
                    playlistSongAlbumArtistMusicGenreElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getMusicGenreId().getMusicGenreName()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistMusicGenreElement);

                    Element playlistSongAlbumArtistBirthPlaceElement = doc.createElement("BirthPlace");
                    playlistSongAlbumArtistBirthPlaceElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getBirthPlace()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistBirthPlaceElement);

                    Element playlistSongAlbumArtistBirthdayElement = doc.createElement("Birthday");
                    playlistSongAlbumArtistBirthdayElement.appendChild(doc.createTextNode(dateFormater2.format(objPlaylistSong.getSongId().getAlbumId().getArtistId().getBirthday())));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistBirthdayElement);

                    Element playlistSongAlbumArtistCreatedByElement = doc.createElement("CreatedBy");
                    playlistSongAlbumArtistCreatedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getCreatedBy()));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistCreatedByElement);

                    Element playlistSongAlbumArtistCreatedDateElement = doc.createElement("CreatedDate");
                    playlistSongAlbumArtistCreatedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getArtistId().getCreatedDate())));
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistCreatedDateElement);

                    Element playlistSongAlbumArtistModifiedByElement = doc.createElement("ModifiedBy");
                    if (objPlaylistSong.getSongId().getAlbumId().getArtistId().getModifiedBy() != null) {
                        playlistSongAlbumArtistModifiedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getArtistId().getModifiedBy()));
                    }
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistModifiedByElement);

                    Element playlistSongAlbumArtistModifiedDateElement = doc.createElement("ModifiedDate");
                    if (objPlaylistSong.getSongId().getAlbumId().getArtistId().getModifiedDate() != null) {
                        playlistSongAlbumArtistModifiedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getArtistId().getModifiedDate())));
                    }
                    playlistSongAlbumArtistElement.appendChild(playlistSongAlbumArtistModifiedDateElement);
                }

                Element playlistSongAlbumMusicGroupElement = doc.createElement("MusicGroup");
                playlistSongAlbumElement.appendChild(playlistSongAlbumMusicGroupElement);

                if (objPlaylistSong.getSongId().getAlbumId().getMusicGroupId() != null) {
                    Attr playlistSongAlbumMusicGroupIDAttribute = doc.createAttribute("ID");
                    playlistSongAlbumMusicGroupIDAttribute.setValue(String.valueOf(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getMusicGroupId()));
                    playlistSongAlbumMusicGroupElement.setAttributeNode(playlistSongAlbumMusicGroupIDAttribute);

                    Element playlistSongAlbumMusicGroupNameElement = doc.createElement("Name");
                    playlistSongAlbumMusicGroupNameElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getMusicGroupName()));
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupNameElement);

                    Element playlistSongAlbumMusicGroupFormationDateElement = doc.createElement("FormationDate");
                    playlistSongAlbumMusicGroupFormationDateElement.appendChild(doc.createTextNode(dateFormater2.format(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getFormationDate())));
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupFormationDateElement);

                    Element playlistSongAlbumMusicGroupCreatedByElement = doc.createElement("CreatedBy");
                    playlistSongAlbumMusicGroupCreatedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getCreatedBy()));
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupCreatedByElement);

                    Element playlistSongAlbumMusicGroupCreatedDateElement = doc.createElement("CreatedDate");
                    playlistSongAlbumMusicGroupCreatedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getCreatedDate())));
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupCreatedDateElement);

                    Element playlistSongAlbumMusicGroupModifiedByElement = doc.createElement("ModifiedBy");
                    if (objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getModifiedBy() != null) {
                        playlistSongAlbumMusicGroupModifiedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getModifiedBy()));
                    }
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupModifiedByElement);

                    Element playlistSongAlbumMusicGroupModifiedDateElement = doc.createElement("ModifiedDate");
                    if (objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getModifiedDate() != null) {
                        playlistSongAlbumMusicGroupModifiedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getMusicGroupId().getModifiedDate())));
                    }
                    playlistSongAlbumMusicGroupElement.appendChild(playlistSongAlbumMusicGroupModifiedDateElement);
                }

                Element playlistSongAlbumCreatedByElement = doc.createElement("CreatedBy");
                playlistSongAlbumCreatedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getCreatedBy()));
                playlistSongAlbumElement.appendChild(playlistSongAlbumCreatedByElement);

                Element playlistSongAlbumCreatedDateElement = doc.createElement("CreatedDate");
                playlistSongAlbumCreatedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getCreatedDate())));
                playlistSongAlbumElement.appendChild(playlistSongAlbumCreatedDateElement);

                Element playlistSongAlbumModifiedByElement = doc.createElement("ModifiedBy");
                if (objPlaylistSong.getSongId().getAlbumId().getModifiedBy() != null) {
                    playlistSongAlbumModifiedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getAlbumId().getModifiedBy()));
                }
                playlistSongAlbumElement.appendChild(playlistSongAlbumModifiedByElement);

                Element playlistSongAlbumModifiedDateElement = doc.createElement("ModifiedDate");
                if (objPlaylistSong.getSongId().getAlbumId().getModifiedDate() != null) {
                    playlistSongAlbumModifiedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getAlbumId().getModifiedDate())));
                }
                playlistSongAlbumElement.appendChild(playlistSongAlbumModifiedDateElement);

                Element playlistSongCreatedByElement = doc.createElement("CreatedBy");
                playlistSongCreatedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getCreatedBy()));
                playlistSongElement.appendChild(playlistSongCreatedByElement);

                Element playlistSongCreatedDateElement = doc.createElement("CreatedDate");
                playlistSongCreatedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getCreatedDate())));
                playlistSongElement.appendChild(playlistSongCreatedDateElement);

                Element playlistSongModifiedByElement = doc.createElement("ModifiedBy");
                if (objPlaylistSong.getSongId().getModifiedBy() != null) {
                    playlistSongModifiedByElement.appendChild(doc.createTextNode(objPlaylistSong.getSongId().getModifiedBy()));
                }
                playlistSongElement.appendChild(playlistSongModifiedByElement);

                Element playlistSongModifiedDateElement = doc.createElement("ModifiedDate");
                if (objPlaylistSong.getSongId().getModifiedDate() != null) {
                    playlistSongModifiedDateElement.appendChild(doc.createTextNode(dateFormater3.format(objPlaylistSong.getSongId().getModifiedDate())));
                }
                playlistSongElement.appendChild(playlistSongModifiedDateElement);
            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);

            StreamResult result;
            result = new StreamResult(new File(this.txtFile.getText()));

            transformer.transform(source, result);

            JOptionPane.showMessageDialog(this, "Η εξαγωγή του αρχείου ολοκληρώθηκε επιτυχώς!", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Παρουσιάστηκε σφάλμα κατά την εξαγωγή του αρχείου!\r\n\r\n" + ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }

    private void importFile() {
        try {
            boolean blnSongExistsInDB = false;
            boolean blnPreviouslyDeleted = false;
            Playlistsong prPlaylistSong;
            int orderNum = 1;

            File fXmlFile = new File(txtFile.getText());
            if (fXmlFile.exists()) {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(fXmlFile);

                doc.getDocumentElement().normalize();

                NodeList songsList = doc.getElementsByTagName("Song");

                if (songsList.getLength() > 0) {

                    Query dbSongsQuery = prEM.createQuery("SELECT s FROM Song s");
                    List<Model.Song> dbSongsList = dbSongsQuery.getResultList();

                    for (int i = 0; i < songsList.getLength(); i++) {
                        Node songNode = songsList.item(i);
                        if (songNode.getNodeType() == Node.ELEMENT_NODE) {
                            Element songElement = (Element) songNode;

                            if (songElement.hasAttribute("ID")) {
                                String songIDAttribute = songElement.getAttribute("ID");

                                txtOutputArea.setText(txtOutputArea.getText() + "\r\nSong ID = " + songIDAttribute + "\r\n");
                                txtOutputArea.setText(txtOutputArea.getText() + "-----------------------------------------------------------------------------------------\r\n");

                                blnSongExistsInDB = false;
                                blnPreviouslyDeleted = false;

                                for (Song dbSong : dbSongsList) {
                                    if (Long.toString(dbSong.getSongId()).equals(songIDAttribute)) {
                                        blnSongExistsInDB = true;

                                        for (Playlistsong objPlaylistsong : this.prFrmMngPlaylist.pbPlaylistSongsToBeDeletedList) {
                                            if (objPlaylistsong.getSongId() == dbSong) {
                                                blnPreviouslyDeleted = true;
                                                this.prFrmMngPlaylist.pbPlaylistSongsToBeDeletedList.remove(objPlaylistsong);
                                                this.prFrmMngPlaylist.playlistSongsList.add(objPlaylistsong);
                                                break;
                                            }
                                        }

                                        try {
                                            if (blnPreviouslyDeleted == false) {
                                                prPlaylistSong = new Playlistsong();
                                                prPlaylistSong.setPlaylistId(prSelectedPlaylist);
                                                prPlaylistSong.setSongId(dbSong);
                                                prPlaylistSong.setCreatedBy(prUsername);

                                                for (Playlistsong objPlaylistsong : prFrmMngPlaylist.playlistSongsList) {
                                                    if (objPlaylistsong.getSongId() == dbSong) {
                                                        throw new Exception("Το τραγούδι '" + dbSong.getSongTitle() + "' έχει προστεθεί ήδη στην συγκεκριμένη λίστα!");
                                                    }
                                                    if (objPlaylistsong.getOrderNum() >= orderNum) {
                                                        orderNum = objPlaylistsong.getOrderNum() + 1;
                                                    }
                                                }
                                                prPlaylistSong.setOrderNum(orderNum);

                                                this.prFrmMngPlaylist.pbPlaylistSongsToBeAddedList.add(prPlaylistSong);

                                                this.prFrmMngPlaylist.playlistSongsList.add(prPlaylistSong);
                                            }

                                            txtOutputArea.setText(txtOutputArea.getText() + "Η προσθήκη του τραγουδιού '" + dbSong.getSongTitle() + "' ολοκληρώθηκε επιτυχώς.\r\n");

                                        } catch (Exception tempEx) {
                                            txtOutputArea.setText(txtOutputArea.getText() + "Η προσθήκη του τραγουδιού '" + dbSong.getSongTitle() + "' δεν ολοκληρώθηκε επιτυχώς.\r\n" + tempEx.getMessage() + "\r\n");
                                        }

                                        this.prFrmMngPlaylist.HandleButtons();

                                        break;
                                    }
                                }

                                if (blnSongExistsInDB == false) {
                                    txtOutputArea.setText(txtOutputArea.getText() + "Δεν βρέθηκε στην βάση δεδομένων\r\n");
                                }
                            }
                        }
                    }
                }

            } else {
                JOptionPane.showMessageDialog(this, "Το αρχείο δεν υπάρχει!", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Παρουσιάστηκε σφάλμα κατά την εισαγωγή των τραγουδιών!\r\n\r\n" + ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }

    }


    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            txtOutputArea.setText("");
            /*
             if (fcXMLFileChooser.getSelectedFile() != null)
             {
             JOptionPane.showMessageDialog(this, fcXMLFileChooser.getSelectedFile().getAbsolutePath(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
             }
             else
             {
             JOptionPane.showMessageDialog(this, "no selected file", this.getTitle(), JOptionPane.ERROR_MESSAGE);
             }
             */

            switch (prEnmXMLActionType) {
                case Export:
                    exportFile();
                    break;
                case Import:
                    importFile();
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnChooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseFileActionPerformed
        try {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle(this.getTitle() + " - Choose File");
            fileChooser.setDialogType(1);
            fileChooser.setAcceptAllFileFilterUsed(false);
            FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", "xml");
            fileChooser.addChoosableFileFilter(filter);

            int status = fileChooser.showOpenDialog(this);
            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                txtFile.setText(selectedFile.getAbsolutePath());
                btnSave.setEnabled(true);
            } else if (status == JFileChooser.CANCEL_OPTION) {
                txtFile.setText("");
                btnSave.setEnabled(false);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnChooseFileActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChooseFile;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblFile;
    private javax.swing.JLabel lblOutput;
    private javax.swing.JTextField txtFile;
    private javax.swing.JTextArea txtOutputArea;
    // End of variables declaration//GEN-END:variables

    @Override
    public Frame getCaller() {
        if (prFrmPlaylists != null) {
            return prFrmPlaylists;
        } else {
            return this.prFrmMngPlaylist;
        }
    }

    @Override
    public Frame getNext() {
        return this.next;
    }

    @Override
    public void setCallersNextNull() {
        ((ApparentModalI) this.prFrmMngPlaylist).setNext(null);
    }

    @Override
    public void setNext(Frame w) {
       throw new UnsupportedOperationException("modal:error"); //To change body of generated methods, choose Tools | Templates.
    }
}
